import React from 'react'
import PropTypes from 'prop-types'
import {Avatar, Table} from 'antd'
import config from 'config'
import {Link} from 'react-router-dom'
import classnames from 'classnames'
import {getCustomerLink, getOperatorLink} from 'utils/tabs'
import AnimTableBody from 'components/DataTable/AnimTableBody'
import styles from './List.less'

const BargainList = ({
                location,
                loading,
                openTab,
                dispatch,
                bargainList,
                bargainPagination,
                isMotion,
              }) => {

  const tableProps = {
    rowKey: 'id',
    dataSource: bargainList,
    pagination: bargainPagination,
    loading: loading.effects['bargainModel/query'],
    location,
  }

  const getAvatarText = (record) => {
    const {nickName, operatorWxId} = record
    if (nickName) {
      return nickName.substr(nickName.length - 1, nickName.length)
    } else {
      return operatorWxId.substr(0, 1).toUpperCase()
    }
  }

  const columns = [
    {
      title: '头像',
      dataIndex: 'icon',
      key: 'icon',
      width: 100,
      fixed: 'left',
      render: (avatar, record, index) => avatar ? <Avatar src={avatar} size="large"/> :
        <Avatar style={{backgroundColor: config.randomColor(index), verticalAlign: 'middle'}}
                size="large">{getAvatarText(record)}</Avatar>,
    }, {
      title: '好友微信号',
      dataIndex: 'dfriendWxId',
      key: 'dfriendWxId',
      width: 120,
      fixed: 'left',
      render: (val, record) => getCustomerLink({
        cid: record.id,
        owxId: record.operatorWxId.wxId,
        name: record.friendRemark || record.friendNickname,
        val,
      }, dispatch),
    }, {
      title: '好友昵称',
      dataIndex: 'friendNickname',
      key: 'friendNickname',
      width: 120,
    }, {
      title: '好友备注',
      dataIndex: 'friendRemark',
      key: 'friendRemark',
      width: 120,
      render: val => val ? val : '-',
    }, {
      title: '归属微信',
      dataIndex: 'operatorWxId',
      key: 'operatorWxId',
      width: 120,
      render: (val) => getOperatorLink({wxId: val.wxId, wxNo: val.wxNo}, dispatch),
    }, {
      title: '归属员工',
      dataIndex: 'operatorWxId',
      key: 'belongEmpName',
      width: 150,
      render: (val) => {
        if (val && val.belongEmp) {
          return val.belongEmp.name
        }

        return '-'
      },
    }, {
      title: '公司部门',
      dataIndex: 'operatorWxId',
      key: 'deptName',
      width: 180,
      render: (val) => {
        if (val && val.dept) {
          return val.dept.name
        }

        return '-'
      },
    }, {
      title: '订单次数',
      dataIndex: 'orderCount',
      key: 'orderCount',
      width: 120,
    }, {
      title: '成交总金额',
      dataIndex: 'transactionAmount',
      key: 'transactionAmount',
      render: (val) => (val ? val : 0) + '元',
      width: 150,
    }, {
      title: '聊天记录',
      dataIndex: '',
      key: '',
      width: 180,
      render: () => <Link to={'#'}>详情</Link>,
    }, {
      title: '未联系天数',
      dataIndex: 'uncontactDays',
      key: 'uncontactDays',
      width: 100,
      render: val => (val ? val : 0) + ' 天',
    }, {
      title: '最后跟进时间',
      dataIndex: 'lastFollowUpTime',
      key: 'lastFollowUpTime',
      width: 100,
      render: val => val ? val : '-',
    },
  ]

  const AnimateBody = (props) => {
    return <AnimTableBody {...props} />
  }

  const CommonBody = (props) => {
    return <tbody {...props} />
  }

  return (
    <Table
      {...tableProps}
      className={classnames(styles.table)}
      scroll={{x: 1500}}
      columns={columns}
      simple
      rowKey={record => record.id}
      components={{
        body: {wrapper: isMotion ? AnimateBody : CommonBody},
      }}
    />
  )
}

BargainList.propTypes = {
  onDeleteItem: PropTypes.func,
  onEditItem: PropTypes.func,
  location: PropTypes.object,
}

export default BargainList
